﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{


    public partial class CountForm : Form
    {
        int time;
        int restTime;
        int num;
        int result;
        int count = 1;
        double totalScore = 0;
        double score = 0;

        public CountForm()
        {
            restTime = 0;
            num = 0;
            time = restTime;
            InitializeComponent();
        }

        public CountForm(int time,int number)
        {
            this.restTime = time;
            this.num = number;
            this.time = restTime;
            score = 100.0 / number;//单题的分值
            InitializeComponent();
        }

        //生成随机加减法算式并保存数字和符号
        private String[] getEquation()
        {
            String[] equation = new String[4];
            //获取三个随机数
            Random ran = new Random();
            int numA = ran.Next(1, 1000);//两个数字
            int numB = ran.Next(1, 1000);
            int sym = ran.Next(0, 2);//随机符号（+、-）
            char Symble = '+';
            if (sym == 1)
                Symble = '-';
            equation[0] = numA +" "+ Symble.ToString()+" " + numB + " "+"=";
            equation[1] = numA.ToString();
            equation[2] = numB.ToString();
            equation[3] = Symble.ToString();
            return equation;
        }


        private void TimeLable_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                count++;
                //倒计时归位
                restTime = time;
                TimeLable.Text = restTime.ToString();
                if (count <= num)
                {
                    //判断上一题是否答对
                    int anwser = Convert.ToInt32(resultTextBox.Text);
                    if (anwser == result)
                    {
                        totalScore += score;
                        this.RWLabel.Text = "正确！";
                        this.RWLabel.ForeColor = Color.Green;
                        //答对提示
                    }
                    else
                    {
                        //答错提示
                        this.RWLabel.Text = "错误！";
                        this.RWLabel.ForeColor = Color.Red;
                    }


                    //出下一个题
                    String[] equation = new string[4];
                    equation = getEquation();
                    //获得下一题的正确答案
                    if (equation[3] == "+")
                        result = Convert.ToInt32(equation[1]) + Convert.ToInt32(equation[2]);
                    else
                        result = Convert.ToInt32(equation[1]) - Convert.ToInt32(equation[2]);
                    QuestionLabel.Text = equation[0];
                    //清空上一次作答                
                    resultTextBox.Text = String.Empty;
                }
                //所有题目已出完
                else
                {
                    //统计最后成绩
                    MessageBox.Show("你最后的成绩为：" + totalScore);
                    this.Close();
                }
            } catch (Exception ex) { 
                MessageBox.Show(ex.Message);
            }
        }

        private void CountForm_Load(object sender, EventArgs e)
        {
            //加载倒计时
            TimeLable.Text=restTime.ToString();
            //加载算式
            String[] equation = new string[4];
            equation = getEquation();
            //获得正确答案
            if (equation[3] == "+")
                result = Convert.ToInt32(equation[1]) + Convert.ToInt32(equation[2]);
            else
                result = Convert.ToInt32(equation[1]) - Convert.ToInt32(equation[2]);
            //将题目赋值
            QuestionLabel.Text = equation[0];
        
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            restTime--;
            TimeLable.Text = restTime.ToString();

            //倒计时结束，下一题
            if(restTime==0)
            button1_Click(sender, e);
        }
    }
}
